package com.hdphone.zljutils.impl;

import com.hdphone.zljutils.inter.IMathUtil;

import java.text.DecimalFormat;

/**
 * @author sq
 * @date 2020/10/19
 * @describe
 */
public class MathUtilImpl implements IMathUtil {
    @Override
    public double convert(double value) {
        try {
            long l1 = Math.round(value * 100); // 四舍五入
            double ret = l1 / 100.0; // 注意:使用 100.0 而不是 100
            return ret;
        } catch (Exception e) {
            return value;
        }
    }

    @Override
    public String convertTwo(double value) {
        try {
            DecimalFormat df = new DecimalFormat("#0.00");
            return df.format(value);
        } catch (Exception e) {
            return String.valueOf(value);
        }
    }

    @Override
    public double convertOne(double value) {
        long l1 = Math.round(value * 10); // 四舍五入
        double ret = l1 / 10.0; // 注意:使用 100.0 而不是 100
        return ret;
    }

    @Override
    public boolean isNumberInt(String str) {
        return str.matches("^\\d+$$");
    }

    @Override
    public boolean isNumberFloat(String str) {
        return str.matches("\\d+\\.\\d+$");
    }

    @Override
    public boolean isNumberStartDot(String str) {
        return str.matches("\\.\\d+$");
    }
}
